package com.cn.web.servlet;

import com.alibaba.fastjson.JSON;
import com.cn.pojo.Brand;
import com.cn.service.BrandService;
import com.cn.service.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    private BrandService brandService = new BrandServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("你好");
        //1.接收品牌数据
        BufferedReader reader = req.getReader();
        //2.JSON字符串
        String param = reader.readLine();
        //3.转换为Brand对象
        Brand brand = JSON.parseObject(param, Brand.class);
        //4.调用service添加】
        brandService.add(brand);
        //5.响应成功的标识
        resp.getWriter().write("success");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
